//index.js
// 导入时间js
const util = require('../../utils/util.js');
const API = require('../../utils/ajax.js')
//获取应用实例
const app = getApp()
Page({
  data: {
    navData: [
      { text: '8月1日', date: '2020-08-01' },
      { text: '8月2日', date: '2020-08-02' },
      { text: '8月3日', date: '2020-08-03' },
      { text: '8月4日', date: '2020-08-04' },
      { text: '8月5日', date: '2020-08-05' },
      { text: '8月6日', date: '2020-08-06' },
      { text: '8月7日', date: '2020-08-07' },
      { text: '8月8日', date: '2020-08-08' },
      { text: '8月9日', date: '2020-08-09' },
      { text: '8月10日', date: '2020-08-10' },
      { text: '8月11日', date: '2020-08-11' },
      { text: '8月12日', date: '2020-08-12' },
      { text: '8月13日', date: '2020-08-13' },
      { text: '8月14日', date: '2020-08-14' },
      { text: '8月15日', date: '2020-08-15' },
      { text: '8月16日', date: '2020-08-16' },
      { text: '8月17日', date: '2020-08-17' },
      { text: '8月18日', date: '2020-08-18' },
      { text: '8月19日', date: '2020-08-19' },
      { text: '8月20日', date: '2020-08-20' },
    ],
    date: '2020-08-01',
    alertData: [
      {
        id: '1',
        img_url: '/assets/trucks.png',
        car_number: 'APK087',
        title: '高温报警'
      },
      {
        id: '2',
        img_url: '/assets/trucks.png',
        car_number: 'APK088',
        title: '高温报警'
      },
      {
        id: '3',
        img_url: '/assets/trucks.png',
        car_number: 'APK090',
        title: '高温报警'
      },
      {
        id: '4',
        img_url: '/assets/trucks.png',
        car_number: 'APK091',
        title: '高温报警'
      }
    ],
    carInfo: [

    ],//车辆历史报警数据
    motto: 'Hello World',
    userInfo: {},
    hasUserInfo: false,
    canIUse: wx.canIUse('button.open-type.getUserInfo'),
    currentTab: 0,
    navScrollLeft: 0
  },
  //事件处理函数
  onLoad: function (option) {
    this.init();
    this.getCarInfo()
  },
  async switchNav(event) {
    var date = event.currentTarget.dataset.date;
    console.log(date)
    var cur = event.currentTarget.dataset.current;
    //每个tab选项宽度占1/5
    var singleNavWidth = this.data.windowWidth / 5;
    //tab选项居中                            
    this.setData({
      navScrollLeft: (cur - 2) * singleNavWidth
    })
    if (this.data.currentTab == cur) {
      return false;
    } else {
      this.setData({
        currentTab: cur
      })
    }
    const res = await API.getVehicleHistory(date);
    let carInfo = [];
    if (res.code === 0) {
      const _carInfo = res.data.filter(ele => (ele.workunitName === '白云佳誉'))
      carInfo = carInfo.concat(_carInfo)
    }
    this.setData({
      carInfo: carInfo
    })
  },
  switchTab(event) {
    var cur = event.detail.current;
    var singleNavWidth = this.data.windowWidth / 5;
    this.setData({
      currentTab: cur,
      navScrollLeft: (cur - 2) * singleNavWidth
    });

  },
  init() {
    // 调用函数时，传入new Date()参数，返回值是日期和时间
    var time = util.formatTime(new Date());
    // 再通过setData更改Page()里面的data，动态更新页面的数据
    this.setData({
      time: time
    });
    if (app.globalData.userInfo) {
      this.setData({
        userInfo: app.globalData.userInfo,
        hasUserInfo: true
      })

    } else if (this.data.canIUse) {
      // 由于 getUserInfo 是网络请求，可能会在 Page.onLoad 之后才返回
      // 所以此处加入 callback 以防止这种情况
      app.userInfoReadyCallback = res => {
        this.setData({
          userInfo: res.userInfo,
          hasUserInfo: true
        })
      }
    } else {
      // 在没有 open-type=getUserInfo 版本的兼容处理
      wx.getUserInfo({
        success: res => {
          app.globalData.userInfo = res.userInfo
          this.setData({
            userInfo: res.userInfo,
            hasUserInfo: true
          })
        }
      })
    }
    wx.getSystemInfo({
      success: (res) => {
        this.setData({
          pixelRatio: res.pixelRatio,
          windowHeight: res.windowHeight,
          windowWidth: res.windowWidth
        })
      },
    })
  },
  async getCarInfo() {
    var date = '2020-08-01';
    const res = await API.getVehicleHistory(date);
    let carInfo = [];
    if (res.code === 0) {
      const _carInfo = res.data.filter(ele => (ele.workunitName === '白云佳誉'))
      carInfo = carInfo.concat(_carInfo)
    }
    this.setData({
      carInfo: carInfo
    })
    console.log(carInfo)
  },
})